package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApUnlikesBehaviorMapper;
import com.usian.behavior.service.ApUnlikesBehaviorService;
import com.usian.model.behavior.dtos.UnLikesBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApUnlikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper,ApUnlikesBehavior> implements ApUnlikesBehaviorService {

    @Autowired
    private ApUnlikesBehaviorMapper apUnlikesBehaviorMapper;

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    @Override/*保存不喜欢的行为*/
    public ResponseResult readBehavior(UnLikesBehaviorDto dto) {

        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        /*获取用户id 用来查询实体*/
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }
        /*查询实体*/
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.findApBehaviorEntry(user.getId(), null);
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"实体不存在");
        }

        /*判重*/
        List<ApUnlikesBehavior> apUnlikesBehaviorList = apUnlikesBehaviorMapper.selectList(Wrappers.<ApUnlikesBehavior>lambdaQuery().eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getId())
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId()));


        /*没查到就添加*/
        if (apUnlikesBehaviorList.size()==0) {
            if (dto.getType() == 0) {
                ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
                apUnlikesBehavior.setArticleId(dto.getArticleId());
                apUnlikesBehavior.setType(dto.getType().intValue());
                apUnlikesBehavior.setCreatedTime(new Date());
                apUnlikesBehavior.setEntryId(apBehaviorEntry.getId());
                this.save(apUnlikesBehavior);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }


        if (apUnlikesBehaviorList.size()==1) {

            ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorList.get(0);
            apUnlikesBehavior.setType(dto.getType().intValue());
            this.updateById(apUnlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
